package com.jsbj.tools;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个推消息推送
 *
 * @created 2019/12/22
 *
 * @author JiangMing
 *
 */
public class AppPush {

	private static String url = "http://sdk.open.api.igexin.com/apiex.htm";

	public static String push(String title, String cid, String text, String objectId, Object phoneType,String tenantCode, String appKey, String masterSecret, String appId) throws Exception {
		String result = "";
		try {
//			System.out.println(phoneType.toString());
			if (phoneType != null && StringUtils.isNotBlank(phoneType.toString())) {
				if (StringUtils.equals(phoneType.toString(), "2")) {
					// iphone透传
					result = AppPush.pushSingleIos(title, cid, text, objectId,tenantCode, appKey, masterSecret, appId);
				}
				if (StringUtils.equals(phoneType.toString(), "1")) {
					// android普通推送
					result = AppPush.pushSingleAndroid(title, cid, text, objectId,tenantCode, appKey, masterSecret, appId);
				}
			} else {
				result = AppPush.pushSingleIos(title, cid, text, objectId,tenantCode, appKey, masterSecret, appId);
			}

		} catch (Exception e) {
			result = "推送异常";
			e.printStackTrace();
		}

		return result;
	}

	public static String pushSingleAndroid(String title, String cid, String text, String objectId,String tenantCode, String appKey, String masterSecret, String appId) throws Exception {
		try {
			IGtPush push = new IGtPush(url, appKey, masterSecret);
			NotificationTemplate template = linkTemplateDemo(title, text, objectId,tenantCode, appKey, masterSecret, appId);
			template.setAPNInfo(getAPNPayload(text, objectId + "-" + tenantCode));

			SingleMessage message = new SingleMessage();
			message.setOffline(true);
			// 离线有效时间，单位为毫秒，可选
			message.setOfflineExpireTime(24 * 3600 * 1000);
			message.setData(template);
			// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
			message.setPushNetWorkType(0);
			Target target = new Target();
			target.setAppId(appId);
			target.setClientId(cid);
			// target.setAlias(Alias);
			IPushResult ret = null;
			ret = push.pushMessageToSingle(message, target);

			if (ret != null) {
				return ret.getResponse().toString();
			} else {
				return "推送失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "服务器响应异常";
		}
	}

	public static String pushSingleIos(String title, String cid, String text, String objectId,String tenantCode, String appKey, String masterSecret, String appId) {

		try {
			IGtPush push = new IGtPush(url, appKey, masterSecret);
			// NotificationTemplate template = getNotificationTemplate(title,
			// text, objectId, appKey, masterSecret, appId);

			TransmissionTemplate template = new TransmissionTemplate();

			template.setAppId(appId);

			template.setAppkey(appKey);

			template.setTransmissionContent("{title:\"" + title + "\",content:\"" + text + "\"," + "payload:\"" + objectId + "-" + tenantCode + "\"}");

			template.setTransmissionType(2);
			template.setAPNInfo(getAPNPayload(text, objectId + "-" + tenantCode)); // ios消息推送

			SingleMessage message = new SingleMessage();
			message.setOffline(true);
			// 离线有效时间，单位为毫秒
			message.setOfflineExpireTime(24 * 3600 * 1000);
			message.setData(template);
			// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
			message.setPushNetWorkType(0);
			Target target = new Target();
			target.setAppId(appId);
			target.setClientId(cid);
			// target.setAlias(Alias);
			IPushResult ret = null;

			try {
				ret = push.pushMessageToSingle(message, target);
			} catch (RequestException e) {
				e.printStackTrace();
				ret = push.pushMessageToSingle(message, target, e.getRequestId());
			}
			if (ret != null) {
				return ret.getResponse().toString();
			} else {
				return "推送失败";
			}

		} catch (Exception e) {
			e.printStackTrace();
			return "服务器响应异常";
		}
	}

	public static Map<String, String> pushAll(String title, String text, String appKey, String masterSecret, String appId) {

		Map<String, String> map = new HashMap<String, String>();
		try {
			IGtPush push = new IGtPush(url, appKey, masterSecret);
			Style0 style = new Style0();
			// STEP2：设置推送标题、推送内容
			style.setTitle(title);
			style.setText(text);
			style.setLogo("push.png"); // 设置推送图标
			// STEP3：设置响铃、震动等推送效果
			style.setRing(true); // 设置响铃
			style.setVibrate(true); // 设置震动

			// STEP4：选择通知模板
			NotificationTemplate template = new NotificationTemplate();
			template.setAppId(appId);
			template.setAppkey(appKey);
			template.setStyle(style);

			// STEP5：定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
			List<String> appIds = new ArrayList<String>();
			appIds.add(appId);
			AppMessage message = new AppMessage();
			message.setData(template);
			message.setAppIdList(appIds);
			message.setOffline(true);
			message.setOfflineExpireTime(1000 * 600); // 时间单位为毫秒

			// STEP6：执行推送
			IPushResult ret = push.pushMessageToApp(message);

			System.out.println(ret.getResponse().toString());
			map.put("result", ret.getResponse().toString());

		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

	private static APNPayload getAPNPayload(String text, String msg) {
		APNPayload payload = new APNPayload();
		// 在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
		// payload.setAutoBadge("+1");
		payload.setContentAvailable(0);
		// ios 12.0 以上可以使用 Dictionary 类型的 sound
		payload.setSound("default");
		payload.setCategory("$由客户端定义");
		payload.addCustomMsg("payload", msg);

		// 简单模式APNPayload.SimpleMsg
		payload.setAlertMsg(new APNPayload.SimpleAlertMsg(text));

		return payload;
	}

	public static NotificationTemplate getNotificationTemplate(String title, String text, String objectId,String tenantCode, String appKey, String masterSecret, String appId) {
		NotificationTemplate template = new NotificationTemplate();
		// 设置APPID与APPKEY
		template.setAppId(appId);
		template.setAppkey(appKey);

		Style0 style = new Style0();
		// 设置通知栏标题与内容
		style.setTitle(title);
		style.setText(text);
		// 配置通知栏图标
		style.setLogo("icon.png");
		// 配置通知栏网络图标
		style.setLogoUrl("");
		// 设置通知是否响铃，震动，或者可清除
		style.setRing(true);
		style.setVibrate(true);
		style.setClearable(true);

		template.setStyle(style);

		template.setTransmissionType(1); // 透传消息接受方式设置，1：立即启动APP，2：客户端收到消息后需要自行处理
		String content = "{title:\"" + title + "\",content:\"" + text + "\"," + "payload:\"" + objectId + "-"+tenantCode+"\"}";
		System.out.println(content);
		template.setTransmissionContent(content);
		return template;
	}

	public static NotificationTemplate linkTemplateDemo(String title, String text, String objectId,String tenantCode, String appKey, String masterSecret, String appId) {
		// LinkTemplate template = new LinkTemplate();
		NotificationTemplate template = new NotificationTemplate();
		// 设置APPID与APPKEY
		template.setAppId(appId);
		template.setAppkey(appKey);
		Style0 style = new Style0();
		// 设置通知栏标题与内容
		style.setTitle(title);
		style.setText(text);
		// 配置通知栏图标
		style.setLogo("icon.png");
		// 配置通知栏网络图标
		style.setLogoUrl("");
		// 设置通知是否响铃，震动，或者可清除
		style.setRing(true);
		style.setVibrate(true);
		style.setClearable(true);
		template.setStyle(style);
		// 设置打开的网址地址
		template.setTransmissionType(2);
		String content = "{title:\"" + title + "\",content:\"" + text + "\"," + "payload:\"" + objectId + "-"+tenantCode+"\"}";
		System.out.println(content);
		template.setTransmissionContent(content);
		return template;
	}

}
